package com.hitqz.robot.biz.filter;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.filter.Filter;
import ch.qos.logback.core.spi.FilterReply;
import com.hitqz.robot.api.common.enums.SubscribeTopicEnum;
import com.hitqz.robot.biz.websocket.util.BroadcastTopicUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author xupkun
 * @date 2024/5/24
 */
@Service
@Slf4j
public class LogFilter extends Filter<ILoggingEvent> {
    @Override
    public FilterReply decide(ILoggingEvent iLoggingEvent) {
        if (iLoggingEvent.getLevel() == Level.INFO) {
            BroadcastTopicUtil.broadcastMessageByTopic(SubscribeTopicEnum.ROBOT_PUSH_INFO_LOG.getTopic(), iLoggingEvent.getMessage());
        }

        if (iLoggingEvent.getLevel() == Level.ERROR) {
            BroadcastTopicUtil.broadcastMessageByTopic(SubscribeTopicEnum.ROBOT_PUSH_ERROR_LOG.getTopic(), iLoggingEvent.getMessage());
        }

        if (iLoggingEvent.getLevel() == Level.WARN) {
            BroadcastTopicUtil.broadcastMessageByTopic(SubscribeTopicEnum.ROBOT_PUSH_WARN_LOG.getTopic(), iLoggingEvent.getMessage());
        }
        //log.info(iLoggingEvent.getMessage());
        return FilterReply.ACCEPT;
    }
}
